<?php
namespace common\models\datas;
/**
 * Created by PhpStorm.
 * User: dongxiaofeng
 * Date: 2017/2/22
 * Time: 12:00
 */
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use Yii;

class CustomerBranchaccount extends \common\components\bases\BaseModel{

    public $confirm_pay_password;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer_branchaccount}}';
    }

    public function rules()
    {
        return [
            [['customer_id','site_id','mode'], 'required'],
            [['scholarship_available', 'scholarship_frozen', 'cash','deposit','grade_id','recommended_times','music_cash','puzzle_cash','body_cash'], 'number'],
            ['is_default','integer'],
            ['pay_password','string'],
            [['pay_password'], 'required', 'on' => ['setpass']],
            [['pay_password'], 'string','max' => 60, 'min' => 6, 'on' => ['setpass']],
            [['confirm_pay_password'], 'validate_confirm_pay_password', 'skipOnEmpty'=>false, 'on' => ['setpass']],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'site_id' => Yii::t('common', '分店id'),//
            'customer_id' => Yii::t('common', '用户id'),//选择，缓存
            'scholarship_available' => Yii::t('common', '可用奖学金'),
            'scholarship_frozen' => Yii::t('common', '冻结奖学金'),
            'cash' => Yii::t('common', '现金'),
            'deposit' => Yii::t('common', '保证金'),
            'is_default' => Yii::t('common', '默认激活的分账户（0非 1是）'),
            'mode' => Yii::t('common', '所属类型（1共同体 2综合体 3管弦乐）'),
            'grade_id' => Yii::t('common', '会员等级'),
            'recommended_times' => Yii::t('common','推荐次数'),
            'created_at' => Yii::t('common', '创建时间'),
            'updated_at' => Yii::t('common', '更改时间'),
            'pay_password' => Yii::t('common', '支付密码'),
            'music_cash' => Yii::t('common', '音乐类型的可用金'),
            'body_cash' => Yii::t('common', '形体类型的可用金'),
            'puzzle_cash' => Yii::t('common', '益智类型的可用金'),
            'confirm_pay_password' => Yii::t('common', '确认支付密码'),
        ];
    }
    public function fields(){
        $fields = parent::fields();
        $fields['grade_id'] = function (){
             switch ($this->grade_id){
                 case 1:
                   return \Yii::t('common', '普通会员');
                 case 2:
                     return \Yii::t('common', 'VIP会员');
             }
        };
        return $fields;
    }

    public function validate_confirm_pay_password($attribute)
    {
        if($this->pay_password != null || $this->pay_password != ''){
            if($this->pay_password != $this->$attribute)
                $this->addError($attribute, Yii::t('common', '两次密码不一致！'));
        }
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],

        ]);
    }
}